/**
 *Copyright (c) 2024 watereyes
 * safetymanagement is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.scene.controller;

import com.koron.common.Excel;
import com.koron.common.StaffAttribute;
import com.koron.common.bean.StaffBean;
import com.koron.common.permission.SPIAccountAnno;
import com.koron.common.web.Util;
import com.koron.scene.domain.Project;
import com.koron.scene.domain.ProjectEnterpriseVo;
import com.koron.scene.service.api.ProjectService;
import com.koron.scene.utils.BusinessCode;
import com.koron.system.annotation.Log;
import com.koron.system.discrete.BusinessManipulation;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;
import springfox.documentation.annotations.ApiIgnore;

import java.io.File;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "Project", description = "项目管理")
@RestController
@RequestMapping("/project")
public class ProjectController {

    private final static Logger logger = LoggerFactory.getLogger(ProjectController.class);
    @Autowired
    private ProjectService projectService;

    @Autowired
    private Util util;

    @RequestMapping(value = "/add.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目管理-保存项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "单位code", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "单位名称", dataType = "String"),
            @ApiImplicitParam(name = "projectType", value = "项目类型", dataType = "String"),
            @ApiImplicitParam(name = "projectLeaderCode", value = "项目负责人code", dataType = "String"),
            @ApiImplicitParam(name = "projectLeaderName", value = "项目负责人名称", dataType = "String"),
            @ApiImplicitParam(name = "establishDate", value = "立项日期", dataType = "date"),
            @ApiImplicitParam(name = "state", value = "状态:0.在建,1.完工", dataType = ""),
            @ApiImplicitParam(name = "description", value = "项目简介", dataType = "String"),
            @ApiImplicitParam(name = "files", value = "项目材料", dataType = "file"),
            @ApiImplicitParam(name = "projectEnterpriseListStr", value = "相关方集合字符串" , dataType = "String"),
    })
    @Log(title = "新增相关方项目信息", businessModule = "项目相关方", businessManipulation = BusinessManipulation.INSERT)
    public MessageBean<?> add(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                              @RequestBody MultipartFile[] files, String name, @RequestParam String corpCode, String corpName,
                              String projectType, String projectLeaderCode, String projectLeaderName,
                              String establishDate, Integer state, String description, String projectEnterpriseListStr) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.add(user, files, name, corpCode, corpName, projectType, projectLeaderCode, projectLeaderName,
                establishDate, state, description, projectEnterpriseListStr);
    }

    @RequestMapping(value = "/update.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目管理-修改项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "name", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "单位code", dataType = "String"),
            @ApiImplicitParam(name = "corpName", value = "单位名称", dataType = "String"),
            @ApiImplicitParam(name = "projectType", value = "项目类型", dataType = "String"),
            @ApiImplicitParam(name = "projectLeaderCode", value = "项目负责人code", dataType = "String"),
            @ApiImplicitParam(name = "projectLeaderName", value = "项目负责人名称", dataType = "String"),
            @ApiImplicitParam(name = "establishDate", value = "立项日期", dataType = "date"),
            @ApiImplicitParam(name = "state", value = "状态:0.在建,1.完工", dataType = "integer"),
            @ApiImplicitParam(name = "description", value = "项目简介", dataType = "String"),
            @ApiImplicitParam(name = "projectEnterpriseListStr", value = "项目相关方集合字符串" , dataType = "String"),
            @ApiImplicitParam(name = "keepPathListStr", value = "保留文件path集合字符串" , dataType = "String"),
            @ApiImplicitParam(name = "deletePathListStr", value = "删除文件path集合字符串" , dataType = "String"),
            @ApiImplicitParam(name = "files", value = "项目材料", dataType = "file"),
    })
    @Log(title = "修改项目信息", businessModule = "项目管理", businessManipulation = BusinessManipulation.UPDATE)
    public MessageBean<?> update(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 @RequestBody MultipartFile[] files, String projectId, String name, @RequestParam String corpCode, String corpName,
                                 String projectType, String projectLeaderCode, String projectLeaderName,
                                 String establishDate, Integer state, String description, String projectEnterpriseListStr,
                                 String keepPathListStr, String deletePathListStr, String keepPrePathListStr, String deletePrePathListStr) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.update(user, files, projectId, name, corpCode, corpName, projectType, projectLeaderCode,
                projectLeaderName, establishDate, state, description, projectEnterpriseListStr,
                keepPathListStr, deletePathListStr, keepPrePathListStr, deletePrePathListStr);
    }

    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目管理-删除项目信息")
    @ApiImplicitParam(name = "projectId", value = "项目Id", dataType = "String")
    @Log(title = "删除项目信息", businessModule = "项目管理", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> delete(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                 String projectId, String corpCode) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.delete(user, projectId);
    }

    @RequestMapping(value = "/deleteEnterprise.htm", method = RequestMethod.POST)
    @ApiOperation(value = "项目相关方-删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "相关方id", dataType = "String"),
    })
    @Log(title = "项目相关方-删除", businessModule = "项目相关方", businessManipulation = BusinessManipulation.DELETE)
    public MessageBean<?> deleteEnterprise(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                           String projectId, String corpCode, String enterpriseId) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.deleteEnterprise(user, projectId, enterpriseId);
    }


    @RequestMapping(value = "/list.htm", method = RequestMethod.GET)
    @ApiOperation(value = "项目管理-获取项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "projectType", value = "项目类型", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态:0.在建,1.完工", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> list(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String name, String projectType, Integer state, String corpCode, String projectId, String enterpriseId,String keyword,
                               Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.list(user, name, projectType, state, corpCode, projectId, enterpriseId, keyword, page, pageSize);
    }

    @RequestMapping(value = "/projectEnterpriseList.htm", method = RequestMethod.GET)
    @ApiOperation(value = "项目相关方-获取项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseName", value = "相关方名", dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "keyword", value = "关键词", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer")
    })
    public MessageBean<?> projectEnterpriseList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                String enterpriseName, String projectName, String corpCode,String keyword,
                                                Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.projectEnterpriseList(user, enterpriseName, projectName, corpCode,keyword, page, pageSize);
    }

    @RequestMapping(value = "/detail.htm", method = RequestMethod.GET)
    @ApiOperation(value = "企业信息-点击获取项目详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
    })
    public MessageBean<?> detail(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, String projectId) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.detail(user, projectId);
    }


    @ApiOperation("下载模板")
    @RequestMapping(value = "/exportTemplate.htm", method = {RequestMethod.GET})
    public HttpEntity<?> exportExcelTemplate(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (null == user) {
            return null;
        }
        Map<String, Object> map = new HashMap<>();
        File targetFile = new File("static/excel/project.xlsx");
        try {
            InputStream initialStream = getClass().getClassLoader().getResourceAsStream("static/excel/project.xlsx");
            FileUtils.copyInputStreamToFile(initialStream, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("项目管理导入模板", targetFile, "default.btl", map);
    }

    @ApiOperation("导入项目管理")
    @ApiImplicitParams({@ApiImplicitParam(name = "file", value = "上传的excel文件", required = true, paramType = "query", dataType = "file")
    })
    @RequestMapping(value = "/importExcel.htm", method = RequestMethod.POST)
    public MessageBean<?> importExcel(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user, @RequestParam(required = false) String userCode,
                                      @RequestParam final MultipartFile file) {

        if (null == user) {
            //用于解决postman测试文件上传试的登陆问题
            List<StaffBean> staffBeanList = (List<StaffBean>) util.staff(userCode, null).getData();
            if (null != staffBeanList && staffBeanList.size() > 0) {
                user = staffBeanList.get(0);
            }
        }
        MessageBean msg = new MessageBean<>();
        if (null == user) {
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        if (null == file) {
            msg.setCode(BusinessCode.FILE_IS_EMPTY.getCode());
            msg.setDescription(BusinessCode.FILE_IS_EMPTY.getDescription());
            return msg;
        }
        return projectService.importExcel(user, file);
    }



    @ApiOperation("项目管理列表导出接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "projectType", value = "项目类型", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态:0.在建,1.完工", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
    })
    @RequestMapping(value = "/exportList.htm", method = RequestMethod.GET)
    public HttpEntity<?> export(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                String name, String projectType, Integer state, String corpCode,
                                String projectId, String enterpriseId) {
        if (null == user) {
            return null;
        }
        List<Project> list = projectService.exportList(user, corpCode, name,projectType,state,projectId,enterpriseId);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/projectList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/projectList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("项目管理列表", targetFile, "projectList.btl", map);
    }


    @RequestMapping(value = "/exportProjectEnterpriseList.htm", method = RequestMethod.GET)
    @ApiOperation(value = "项目相关方列表导出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseName", value = "相关方名", dataType = "String"),
            @ApiImplicitParam(name = "projectName", value = "项目名", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "Integer")
    })
    public HttpEntity<?> exportProjectEnterpriseList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                                                String enterpriseName, String projectName, String corpCode) {
        if (null == user) {
            return null;
        }
        List<ProjectEnterpriseVo> list = projectService.exportProjectEnterpriseList(user, enterpriseName, projectName, corpCode);
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        File targetFile = new File("static/excel/projectEnterpriseList.xlsx");
        try {
            InputStream in = getClass().getClassLoader().getResourceAsStream("static/excel/projectEnterpriseList.xlsx");
            FileUtils.copyInputStreamToFile(in, targetFile);
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
        }
        return Excel.createFile("项目相关方列表", targetFile, "projectEnterpriseList.btl", map);
    }


    @RequestMapping(value = "/getAll.htm", method = RequestMethod.GET)
    @ApiOperation(value = "获取所有的项目数据")
    public MessageBean<?> getAll(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.getAll(user);
    }


    @RequestMapping(value = "/homePageProjectList.htm", method = RequestMethod.POST)
    @ApiOperation(value = "首页工作台工程项目列表接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "项目名称", dataType = "String"),
            @ApiImplicitParam(name = "projectType", value = "项目类型", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态:0.在建,1.完工", dataType = "String"),
            @ApiImplicitParam(name = "corpCode", value = "水司code", dataType = "String"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseId", value = "企业id", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页" , dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数" , dataType = "Integer", paramType = "query")
    })
    public MessageBean<?> homePageProjectList(@SPIAccountAnno @StaffAttribute("_user") @ApiIgnore StaffBean user,
                               String name, String projectType, Integer state, String corpCode, String projectId, String enterpriseId,
                               Integer page, Integer pageSize) {
        if (user == null) {
            MessageBean msg = new MessageBean();
            msg.setCode(BusinessCode.NO_LOGIN.getCode());
            msg.setDescription(BusinessCode.NO_LOGIN.getDescription());
            return msg;
        }
        return projectService.homePageProjectList(user, name, projectType, state, corpCode, projectId, enterpriseId, page, pageSize);
    }

}
